package org.web.clientlistmanager.client.biz;

import com.google.gson.Gson;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.Getter;
import lombok.Setter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.web.base.domain.helper.ServiceExceptionHelper;
import org.web.clientlistmanager.client.config.ClmClientConfig;
import org.web.clientlistmanager.sdk.dto.ClmRequest;
import org.web.clientlistmanager.sdk.event.EventHelper;
import org.web.clientlistmanager.sdk.event.EventInterface;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Component("clmNettyClientHandler")
@Setter
@Getter
@ChannelHandler.Sharable
public class ClmNettyClientHandler extends ChannelInboundHandlerAdapter {

    private static final Logger logger = LogManager.getLogger(ClmNettyClientHandler.class);

    @Autowired
    private BeanFactory beanFactory;

    private ClmClientConfig clmClientConfig;

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) {
        logger.info("ClientSide channelRegistered");
        EventHelper.sendClmRequest(ctx, clmClientConfig.getHeartBeatEventRequest());
        ctx.fireChannelRegistered();
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) {
        logger.info("ClientSide channelUnregistered");
        ctx.fireChannelUnregistered();

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        logger.info("ClientSide channelActive");
        ctx.fireChannelActive();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        logger.info("ClientSide channelInactive");
        ctx.fireChannelInactive();

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        try {
            List<ClmRequest> list = EventHelper.parseRequest(msg);
            for (ClmRequest clmRequest : list) {
                // 从Spring容器中获取对应类型的MessageHandler
                EventInterface event = beanFactory.getBean(clmRequest.getEventName(), EventInterface.class);
                if (event != null) {
                    event.executeRequest(clmRequest, ctx);
                } else {
                    logger.error("erorr is" + clmRequest.getEventName());
                }
            }
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
        }
        ctx.fireChannelRead(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        System.out.println("666666");
        ctx.fireChannelReadComplete();

    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
        logger.info("ClientSide userEventTriggered");
        ctx.fireUserEventTriggered(evt);
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) {
        logger.info("ClientSide channelWritabilityChanged");
        ctx.fireChannelWritabilityChanged();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        logger.info("ClientSide exceptionCaught");
        ctx.fireExceptionCaught(cause);
    }
}
